<template>
  <el-aside width="200px">
    <NotLoginMenu></NotLoginMenu>
  </el-aside>
  <span id="header"><span id="header-header">管理员登录</span>
  <div id="login">
    <el-form :model="Admin" status-icon :rules="rules" ref="Admin" label-width="100px" class="demo-Admin">
      <el-form-item label="账号" prop="account">
        <div id="user"><el-input type="account" v-model="Admin.account" autocomplete="off"></el-input></div>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <span id="passwordMsg"></span>
        <div id="password"><el-input type="password" v-model="Admin.password" autocomplete="off"></el-input></div>
      </el-form-item>
      <div id="subment"><el-button type="primary" @click="submitForm('Admin')">登录</el-button></div>
    </el-form>
  </div>
  </span>

</template>

<script>
import instance from "@/utlis/request.js";
import NotLoginMenu from "@/components/menu/NotLoginMenu";
export default {
  components: {
    NotLoginMenu,
  },
  data() {
    var checkAge = (rule, value, callback) => {
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      }
      else {
        if (this.Admin.password !== '') {
          this.$refs.Admin.validateField('password');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      Admin: {
        account: '',
        password: '',
      },
      rules: {
        account: [
          { validator: validatePass, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(

    ) {
      this.$refs.Admin.validate((valid) => {
        if (valid) {
          //alert(this.Admin.account)
          instance.post("/Manage/Login",this.Admin).then(res=>{
            //if(res.data.success)
              if(res.data.success){
               // console.log(res.data.data[0])后端传过来的管理员信息
                var a=res.data.data[0].id
                localStorage.setItem("token",a)//将数据在保存到token
                //let token = localStorage.getItem("token")将数据从token中取出来
               // console.log(token)
                this.$router.push("/homepage")
              }
              else {
                this.$message.error("账号密码错误！");
                return false;
              }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>



<style scoped>
#login{
  margin-top: 100px;
}
#header-header{
  margin-left: 200px;
}
#subment{
  margin-left: 250px;
}
#header{
  position: absolute;
  top: 200px;
  left: 450px;
  font-size: 30px;
}
#user{
  width: 400px;
}
#password{
  width: 400px;
}
</style>
